package com.jmpt.fangbohui.ui.personalcenter;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jmpt.fangbohui.Constants;
import com.jmpt.fangbohui.R;
import com.jmpt.fangbohui.bean.AlreadyBBClientBean;
import com.jmpt.fangbohui.dao.LoginSQLiteOperator;
import com.jmpt.fangbohui.ui.base.BaseHeadActivity;
import com.jmpt.fangbohui.utils.DebugLog;
import com.jmpt.fangbohui.utils.ToastHelper;

public class AlreadyBBClientActivity extends BaseHeadActivity {

	private ListView lv_activity_already_bb_client;
	private MyClientAdapter adapter;
	public final static int SUCCESS = 200;
	public final static int FAILED = 202;

	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			case SUCCESS:
				String str = (String) msg.obj;
				ToastHelper.ShowToast(str, mContext);
				hideLoading();
				if(bean.done.equals("true")){
					//Set Adapter
					
					adapter = new MyClientAdapter();
					lv_activity_already_bb_client.setAdapter(adapter);
				}
			break;
			case FAILED:
				String str2 = (String) msg.obj;
				ToastHelper.ShowToast(str2, mContext);
				hideLoading();
			break;
			}
			
		};
	};
	private AlreadyBBClientBean bean;
	private TextView tv_manage_house;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		initData();
		initEvent();
	}

	private void initData() {
		//到网上获取已报备列表，
		//展示加载页面
		LoginSQLiteOperator oper = new LoginSQLiteOperator(this);
		final List<String> querySessionId = oper.querySessionId();
		if(querySessionId.size() != 2){
			return;
		}
		showLoading();
		
		
		new Thread(){
			public void run() {
				HttpClient httpClient = new DefaultHttpClient();
				try {
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					params.add(new BasicNameValuePair("AgentId",querySessionId.get(0)));
					
				    HttpPost postMethod = new HttpPost(Constants.URL.ALREADYBBCLIENT);
				    postMethod.setHeader("Cookie", "PHPSESSID=" + querySessionId.get(1)); 
				    postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中
								
				    HttpResponse response = httpClient.execute(postMethod); //执行POST方法
				    DebugLog.i("resCode = " + response.getStatusLine().getStatusCode());
				    
				    if(response.getStatusLine().getStatusCode() != 200){
				    	Message msg = Message.obtain();
					    msg.what = FAILED;
					    msg.obj = "网络暂不畅通，请稍后再试";
					    handler.sendMessage(msg);
				    	return;
				    }
				    parser(EntityUtils.toString(response.getEntity(), "utf-8"));
				    
								
				} catch (Exception e) {
				    e.printStackTrace();
				} 
			};
		}.start();
		
		
//		http.configCookieStore(cookieStore);
//		http.send(HttpMethod.POST, Constants.URL.ALREADYBBCLIENT, new RequestCallBack<String>() {
//
//			@Override
//			public void onFailure(HttpException arg0, String arg1) {
//				DebugLog.i("failure" + arg1);
//				hideLoading();
//			}
//
//			@Override
//			public void onSuccess(ResponseInfo<String> arg0) {
//				DebugLog.i("success" + arg0.result);
//				hideLoading();
//			}
//		});

	}

	private void initEvent() {
		
	}
	


	private void initView() {
		setContentView(R.layout.activity_already_bb_client);
		lv_activity_already_bb_client = (ListView) findViewById(R.id.lv_activity_already_bb_client);
		
		showTitle("已报备客户");
		showLeftImageButton(R.drawable.btn_head_back, new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

	}
	
	private void parser(String str){
		Gson gson = new Gson();
		str = str.substring(str.indexOf("{"));
		DebugLog.i(str);
		bean = gson.fromJson(str, AlreadyBBClientBean.class);
		Message msg = Message.obtain();
	    msg.what = SUCCESS;
	    msg.obj = bean.msg;
	    handler.sendMessage(msg);
	}

	private class MyClientAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return bean.retval.size();
		}

		@Override
		public Object getItem(int arg0) {
			return bean.retval.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ClientHolder holder = null;
			if (convertView != null) {
				holder = (ClientHolder) convertView.getTag();
			} else {
				holder = new ClientHolder();
				convertView = View.inflate(AlreadyBBClientActivity.this,
						R.layout.item_already_bb_client, null);
				holder.tv_item_already_bb_client_name = (TextView) convertView
						.findViewById(R.id.tv_item_already_bb_client_name);
				holder.tv_item_already_bb_client_phone = (TextView) convertView
						.findViewById(R.id.tv_item_already_bb_client_phone);
				holder.tv_item_already_bb_client_purpose = (TextView) convertView
						.findViewById(R.id.tv_item_already_bb_client_purpose);
				holder.tv_item_already_bb_client_progress = (TextView) convertView
						.findViewById(R.id.tv_item_already_bb_client_progress);
				holder.tv_item_already_bb_client_time = (TextView) convertView
						.findViewById(R.id.tv_item_already_bb_client_time);
				holder.tv_item_already_bb_client_broker = (TextView) convertView
						.findViewById(R.id.tv_item_already_bb_client_broker);
				convertView.setTag(holder);
			}
			int indexof = bean.retval.get(position).status.indexOf("(");
			String progress = bean.retval.get(position).status.substring(0,indexof)+"\n"+bean.retval.get(position).status.substring(indexof);
			holder.tv_item_already_bb_client_name.setText(bean.retval.get(position).username);
			holder.tv_item_already_bb_client_phone.setText(bean.retval.get(position).phone);
			holder.tv_item_already_bb_client_purpose.setText(bean.retval.get(position).houseTitle);
			holder.tv_item_already_bb_client_progress.setText(progress);
			holder.tv_item_already_bb_client_time.setText(bean.retval.get(position).time);
			holder.tv_item_already_bb_client_broker.setText(bean.retval.get(position).agentName);
			return convertView;
		}

	}

	public class ClientHolder {
		private TextView tv_item_already_bb_client_name;
		private TextView tv_item_already_bb_client_phone;
		private TextView tv_item_already_bb_client_broker;
		
		private TextView tv_item_already_bb_client_purpose;
		private TextView tv_item_already_bb_client_progress;
		private TextView tv_item_already_bb_client_time;
	}
}
